package top.starmc.starpet.internal

import eos.moe.dragoncore.api.CoreAPI
import ink.ptms.um.Mythic
import taboolib.common.util.random
import taboolib.library.configuration.ConfigurationSection
import taboolib.platform.util.sendLang
import top.starmc.starpet.util.calculate

class SkillData(config: ConfigurationSection) {
    val type = config.getString("type")!!

    private val levelLimit = config.getInt("level")

    private val rate = config.getString("rate")!!

    private val group = config.getString("group")!!

    private val cd = config.getString("cd")!!

    private val key = config.getStringList("key").onEach(CoreAPI::registerKey)

    private val self = config.getString("self")

    private val ownerSkill = config.getString("owner")

    val stats = config.getStringList("stats")

    fun runSkill(data: PlayerData, keys: List<String>?) {
        with(data) {
            if (key.isEmpty() || keys?.equals(key) == true) {
                val time = getCoolDown(group, cd.calculate(level).toLong())
                if (time > 0) {
                    owner.sendLang("2", time)
                    return
                }
                if (level > levelLimit && random(rate.calculate(level).toDouble())) {
                    addCoolDown(group)
                    ownerSkill?.let {
                        Mythic.API.castSkill(owner, it)
                    }
                    if (self != null && entity != null) {
                        Mythic.API.castSkill(entity!!.bukkitEntity, self)
                    }
                    updateSkillStats(this@SkillData)
                }
            }
        }
    }
}